<?php

App::uses('Crud', 'Model');
App::uses('UserTeam', 'Model');

class TeamLine extends Crud
{
    /**
     * Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

    /**
     * Obtenemos las lineas activas asignadas a un grupo
     * @param string $teamId UUID
     */
    public function getEnableByTeam($teamId) {
        $filters = array(
            'fields' => array(
                'TeamLine.id as id',
                'l.name as line_name',
                'l.colour as line_colour',
                'TeamLine.line_id as line_id'
            ),
            'joins' => array(
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'INNER',
                    'conditions' => array(
                        'l.id = TeamLine.line_id',
                    ),
                ),
            ),
            'conditions' => array(
                'TeamLine.team_id' => $teamId,
                'TeamLine.status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'l.name ASC',
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }

    /**
     * Verifica que una linea no este asignada a un equipo
     * @param string $teamId UUID
     * @param string $lineId UUID
     * @return bool true si la linea ya ha  sigo asignada el equipo, false en otros
     * casos
     */
    public function existsEnabledByTeamAndLine($teamId, $lineId) {
        $filters = array(
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'team_id' => $teamId,
                'line_id' => $lineId,
            ),
        );
        $record = $this->find('first', $filters);
        return count($record) > 0;
    }

}
